package cn.tedu.eguser1.controller;

import cn.tedu.eguser1.base.response.JsonResult;
import cn.tedu.eguser1.base.response.StatusCode;
import cn.tedu.eguser1.mapper.UserMapper;
import cn.tedu.eguser1.pojo.dto.UserAddParam;
import cn.tedu.eguser1.pojo.dto.UserLoginParam;
import cn.tedu.eguser1.pojo.dto.UserUpdateParam;
import cn.tedu.eguser1.pojo.entity.User;
import cn.tedu.eguser1.pojo.vo.UserListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("v1/user/")
@Tag(name = "用户管理模块")
@Validated
public class UserController {
    @Autowired
    UserMapper userMapper;
    @PostMapping("add")
    @Operation(summary = "01-用户注册")
    @ApiOperationSupport(order = 10)
    public JsonResult add(@RequestBody @Validated UserAddParam userAddParam)
    {
        log.debug("开始处理【用户注册】的请求，参数：{}", userAddParam);
        User user = new User();
        BeanUtils.copyProperties(userAddParam,user);
        user.setCreateTime(new Date());
        user.setUpdateTime(new Date());
        userMapper.insertUser(user);
        return JsonResult.ok();
    }
    @GetMapping("list")
    @Operation(summary = "02-查询用户列表")
    @ApiOperationSupport(order = 20)
    public JsonResult list()
    {
        System.out.println("前端访问成功");
        return JsonResult.ok(userMapper.listUser());

    }
    @PostMapping("del{id}")
    @Operation(summary = "03-删除用户")
    @ApiOperationSupport(order = 30)
    public JsonResult del(@PathVariable @Range(min = 1L,max = 100L ,
            message = "用户ID必须在1-100之间") Long id)
    {
        log.debug("开始处理【删除用户】的请求，参数：{}", id);
        userMapper.deleteById( id);
        return JsonResult.ok();
    }
    @PostMapping("update")
    @Operation(summary = "04-修改用户")
    @ApiOperationSupport(order = 40)
    public JsonResult update(@RequestBody UserUpdateParam userUpdateParam){
        log.debug("输入："+userUpdateParam);
        User user = new User();
        BeanUtils.copyProperties(userUpdateParam,user);
        user.setUpdateTime(new Date());
        int rows = userMapper.updateUser(user);
        if (rows>0){
            return JsonResult.ok();
        }else {
            return new JsonResult(StatusCode.OPERATION_FAILED);
        }
    }
    @PostMapping("login")
    @Operation(summary = "05-用户登录")
    public JsonResult login(@RequestBody  UserLoginParam userLoginParam){
        System.out.println("用户登录："+userLoginParam);
        return JsonResult.ok();
    }


}
